﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using System.Reflection;
using System.Configuration;
using System.Web.Configuration;

namespace Htss.Platform.Web
{
    /// <summary>
    /// 返回一个视图对象
    /// </summary>
    public class ViewResult:ActionResult
    {
        public ViewResult() { }
        public ViewResult(string _viewClass)
        {
            this.ViewClass = _viewClass;
        }
        /// <summary>
        /// 请求路由指定的命名空间
        /// </summary>
        public string RequestNamespace
        {
            get;
            set;
        }
        /// <summary>
        /// 请求路由盒子名称
        /// </summary>
        public string RequestBoxName
        { 
            get; 
            set; 
        }
        public HtmlHelper HtmlHelper
        {
            get;
            set;
        }
        public string ViewClass { get; set; }
        public override void ExecuteResult(HttpContextBase context)
        {
            Assembly assembly = Assembly.Load(WebConfigurationManager.AppSettings["WebNamespace"]);
            Type type = assembly.GetType(WebConfigurationManager.AppSettings["ViewCodeNamespace"]+"." + ViewClass);

            object obj = Activator.CreateInstance(type);
            //初始化值
            ((ViewPage)obj).Html = HtmlHelper;
            object resultBuilder = null;
            try
            {
                object returnVale = type.InvokeMember("Execute", BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Instance, null, obj, null);
                resultBuilder = type.GetProperty("ResultBuilder", BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty).GetValue(obj, null);
            }
            finally
            {

            }
            ResultObject = resultBuilder;
        }
    }
}
